// keel_sys.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <MMSystem.h>

#include "keel_sys_NativeClock.h"

UINT g_uPeriod;
DWORD (*getTime)(); 

JNIEXPORT jdouble JNICALL Java_keel_sys_NativeClock_init
(JNIEnv *, jobject)
{
	timecaps_tag ptc;
	if ( timeGetDevCaps(&ptc, sizeof(ptc))==TIMERR_NOERROR &&
		ptc.wPeriodMin<10 &&
		timeBeginPeriod(ptc.wPeriodMin)==TIMERR_NOERROR )
	{
		g_uPeriod = ptc.wPeriodMin;
		getTime = timeGetTime;
		return 1000.0;
	}
	else
	{
		g_uPeriod = 0;
		getTime = GetTickCount;
		return 1000.0;
	}
}

JNIEXPORT jlong JNICALL Java_keel_sys_NativeClock_getTime
(JNIEnv *, jobject)
{
	if(getTime)
		return getTime();
	return 0;
}

JNIEXPORT void JNICALL Java_keel_sys_NativeClock_finalizeNative
(JNIEnv *, jobject)
{
	if(g_uPeriod)
		timeEndPeriod(g_uPeriod);
}